package com.example.day9;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.day9.Model.Bean;

import java.util.ArrayList;
import java.util.List;

class RAdapter extends RecyclerView.Adapter {
    private ArrayList<Bean.ResultsBean> list;
    private Context context;

    public RAdapter(Context context, ArrayList<Bean.ResultsBean> list) {
        this.context = context;
        this.list = list;
    }

    public void addData(List<Bean.ResultsBean> results) {
        list.addAll(results);
        notifyDataSetChanged();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView url;
        private TextView desc;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            url = itemView.findViewById(R.id.layout_recycler_url);
            desc = itemView.findViewById(R.id.layout_recycler_desc);
        }
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_recycler, null);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ViewHolder viewHolder1 = (ViewHolder) viewHolder;

        viewHolder1.desc.setText(list.get(i).getDesc());

        Glide.with(context).load(list.get(i).getUrl())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(viewHolder1.url);


    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
